package com.threesome.memos.util;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.threesome.memos.R;

public class AudioRecorderRing extends FrameLayout {

	static final long ANIMATION_INTERVAL = 2000;

	private ImageView backgroundImage;
	private LinearLayout foregroundImageLayout;
	private ImageView foregroundImageUp;
	private ImageView foregroundImageDown;

	int amplitude = 0;
	Recorder mRecorder;

	public AudioRecorderRing(Context context) {
		super(context);
	}

	public AudioRecorderRing(Context paramContext,
			AttributeSet paramAttributeSet) {
		super(paramContext, paramAttributeSet);
	}

	public AudioRecorderRing(Context paramContext,
			AttributeSet paramAttributeSet, int paramInt) {
		super(paramContext, paramAttributeSet, paramInt);
	}

	public void init() {
		backgroundImage = (ImageView) findViewById(R.id.background_image);
		foregroundImageLayout = (LinearLayout) findViewById(R.id.foreground_image);
		foregroundImageUp = (ImageView) findViewById(R.id.foreground_image_up);
		foregroundImageDown = (ImageView) findViewById(R.id.foreground_image_down);
		showAmplitude(0);
	}

	public void showAmplitude(int param) {
		ViewGroup.LayoutParams localLayoutParams = foregroundImageLayout
				.getLayoutParams();
		int d1 = backgroundImage.getHeight();
		localLayoutParams.height = d1;
		localLayoutParams.width = backgroundImage.getWidth();
		foregroundImageLayout.setLayoutParams(localLayoutParams);

		ViewGroup.LayoutParams localLayoutParamsUp = foregroundImageUp
				.getLayoutParams();
		ViewGroup.LayoutParams localLayoutParamsDown = foregroundImageDown
				.getLayoutParams();
		localLayoutParamsUp.height = d1;
		localLayoutParamsUp.width = backgroundImage.getWidth();
		localLayoutParamsDown.height = 0;
		localLayoutParamsDown.width = backgroundImage.getWidth();
		if (param > 800) {
			double d2 = 0;
			if (param > 32768)
				param = 32768;
			d2 = Math.sqrt(param / 32768.0D);
			localLayoutParamsUp.height = (int) ((1.0D - d2) * d1);
			localLayoutParamsDown.height = d1 - localLayoutParamsDown.height;
		}
		foregroundImageUp.setLayoutParams(localLayoutParamsUp);
		foregroundImageDown.setLayoutParams(localLayoutParamsDown);
		foregroundImageDown.setBackgroundColor(R.drawable.red);
		requestLayout();
	}

	public void setRecorder(Recorder recorder) {
		mRecorder = recorder;
		invalidate();
	}
}
